<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class audit_service{
	private $CI;
	function __construct() { 
		$this->CI = &get_instance();
		$this->CI -> load -> model("Dic_model");	
		$this->CI->load->model("Academia_job_model");
		$this ->CI-> load -> model("Subject_platform_model");	
		$this->CI->load->model("Academic_report_model");
		$this->CI->load->model("Audit_model");
		$this->CI->load->model("Awards_model");
		$this->CI->load->model("Communication_model");
		$this->CI->load->model("Horizontal_project_model");
		$this->CI->load->model("Monograph_model");
		$this->CI->load->model("Paper_model");
		$this->CI->load->model("Patent_data_model");
		$this->CI->load->model("Patent_model");
		$this->CI->load->model("Research_report_model");
		$this->CI->load->model("Science_conversion_model");
		$this->CI->load->model("Technical_standard_model");
		$this->CI->load->model("Vertical_project_model");
		$this->CI->load->library('ProjectService/check_service');
	}

	/**  
	* 新增一条审核记录
	* @access public 
	* @param  $type  成果类型
	* @param  $data_id  成果id
	*/  
	function addAudit($type,$data_id){	

		$role=explode(',', getSess()['sub_role_id']);
		array_push($role, getSess()['role_id']);
	
		$sql=$this->CI->sqlEscape("update db_audit set is_read=?? where data_id=?? and type=??",array(CommonWhether::Whether[XPHEnum::Value],$data_id,$type));
		$this->CI->Audit_model->query($sql,false);

		$dic =$this->CI->Dic_model->get_single(array('dic_key'=>'is_config'));
		if($dic['z_index']== CommonWhether::Whether[XPHEnum::Value]){
			exit(retJson('系统已临时关闭,无法提交审核',false));
		}

		$dept_data = array();
		foreach ((new addDept())->getAll() as $k => $v) {
			if($v[XPHEnum::Alternate] > 100 && $v[XPHEnum::Alternate] < 200){
				$dept_data[$k]= $v[XPHEnum::Caption];
			}
		}
	

		// 判断是否已提交审核
		$connar['where']=$this->CI->sqlEscape('data_id=?? and type=?? and (state=?? or state=??) and node=??',array($data_id,$type,AuditState::UnAudit[XPHEnum::Value],AuditState::Pass[XPHEnum::Value],AuditNode::Second[XPHEnum::Value]));
		$audit=$this->CI->Audit_model->get_single_full($connar);

		switch ($type) {
			case ResultsType::First[XPHEnum::Value]:
				$data=$this->CI->Paper_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Second[XPHEnum::Value]:
				$data=$this->CI->Monograph_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Third[XPHEnum::Value]:
				$data=$this->CI->Patent_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Fourth[XPHEnum::Value]:
				$data=$this->CI->Awards_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Fifth[XPHEnum::Value]:
				$data=$this->CI->Research_report_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Sixth[XPHEnum::Value]:
				$data=$this->CI->Technical_standard_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Seventh[XPHEnum::Value]:
				$data=$this->CI->Vertical_project_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Eighth[XPHEnum::Value]:
				$data=$this->CI->Horizontal_project_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Ninth[XPHEnum::Value]:
				$data=$this->CI->Science_conversion_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Tenth[XPHEnum::Value]:
				$data=$this->CI->Communication_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Eleventh[XPHEnum::Value]:
				$data=$this->CI->Academic_report_model->get_single(array('id'=>$data_id));
				break;
			
			case ResultsType::Twelfth[XPHEnum::Value]:
				$data=$this->CI->Academia_job_model->get_single(array('id'=>$data_id));
				break;
			case ResultsType::Thirteen[XPHEnum::Value]:
				$data=$this->CI->Subject_platform_model->get_single(array('id'=>$data_id));
				break;	
		}

		if($audit){
			//当标题不一样的时候改变审核的标题
			if($audit['data_name'] != $data['name']){
				$update1=array('data_id'=>$data_id,'data_name'=>$data['name'],'type'=>$type);
				$audit=$this->CI->Audit_model->update($update1,'data_id');
			}		
			exit(retJson('请勿重复提交审核',false));
		}

		$add=array('admin_id'=>getSess()['id'],'type'=>$type,'data_id'=>$data_id,'data_name'=>$data['name'],'node'=>AuditNode::Second[XPHEnum::Value],'state'=>AuditState::UnAudit[XPHEnum::Value],'created'=>time(),'dept_name'=>$data['dept_name'],'audit_type'=>$data['is_submit_cg'],'examine_level'=>$data['examine_level']);

		// 修改成果状态
		if($data['status']!=ResultState::UnSubmit[XPHEnum::Value] && $data['status']!=ResultState::Fail[XPHEnum::Value] ){
			exit(retJson('当前成果状态不正确无法提交',false));
		}

		// 判断审核节点
		if(in_array(WebRole::SchoolLeader[XPHEnum::Value], $role) || in_array(WebRole::BranchPresident[XPHEnum::Value], $role)){
			$add['node']=AuditNode::Second[XPHEnum::Value];
		}else if(in_array(WebRole::Teacher[XPHEnum::Value], $role)){
			if($data['dept_name'] == Dept::Tenth[XPHEnum::Caption] || $data['dept_name'] == Dept::Eleventh[XPHEnum::Caption] || $data['dept_name'] == Dept::Twelve[XPHEnum::Caption]){
				$add['node']=AuditNode::Second[XPHEnum::Value];
			}else{
				if(in_array($data['dept_name'], $dept_data)){
					$add['node']=AuditNode::Second[XPHEnum::Value];
				}else{
					$add['node']=AuditNode::First[XPHEnum::Value];
				}
			}
		}else{
			exit(retJson('没有提交权限',false));
			
		}

		//修改提交状态为审核中
		$update1 = array('id'=>$data_id,'status'=>ResultState::Auditing[XPHEnum::Value]);
		if(isset($data['only_code'])&& $data['only_code']!=''){
			$star_str = substr($data['only_code'],0,26);
			$end_str = substr($data['only_code'],-5);
			$only_code = $star_str.ResultState::Auditing[XPHEnum::Value].$end_str; //更新审核状态	
			$update1['only_code'] = $only_code;
		}

		switch ($type) {
			case ResultsType::First[XPHEnum::Value]:
				$data=$this->CI->Paper_model->update($update1);
				break;
			
			case ResultsType::Second[XPHEnum::Value]:
				$this->CI->Monograph_model->update($update1);
				break;
			
			case ResultsType::Third[XPHEnum::Value]:
				$this->CI->Patent_model->update($update1);
				break;
			
			case ResultsType::Fourth[XPHEnum::Value]:
				$this->CI->Awards_model->update($update1);
				break;
			
			case ResultsType::Fifth[XPHEnum::Value]:
				$this->CI->Research_report_model->update($update1);
				break;
			
			case ResultsType::Sixth[XPHEnum::Value]:
				$this->CI->Technical_standard_model->update($update1);
				break;
			
			case ResultsType::Seventh[XPHEnum::Value]:
				$this->CI->Vertical_project_model->update($update1);
				break;
			
			case ResultsType::Eighth[XPHEnum::Value]:
				$this->CI->Horizontal_project_model->update($update1);
				break;
			
			case ResultsType::Ninth[XPHEnum::Value]:
				$this->CI->Science_conversion_model->update($update1);
				break;
			
			case ResultsType::Tenth[XPHEnum::Value]:
				$this->CI->Communication_model->update($update1);
				break;
			
			case ResultsType::Eleventh[XPHEnum::Value]:
				$this->CI->Academic_report_model->update($update1);
				break;
			
			case ResultsType::Twelfth[XPHEnum::Value]:
				$this->CI->Academia_job_model->update($update1);
				break;
			case ResultsType::Thirteen[XPHEnum::Value]:
				$this->CI->Subject_platform_model->update($update1);
				break;	
		}		

		$this->CI->Audit_model->add($add);
		
		 return array('status'=>true);		
	}	

	/**  
	* 通过审核
	* @access public 
	* @param  $type  成果类型
	* @param  $data_id  成果id
	*/  
	function pass($type,$data_id,$is_kaohe=false){	
			
		if($is_kaohe){//判断是否仅考核通过
			$auditlist=$this->CI->Audit_model->get_list(array('data_id'=>$data_id,'type'=>$type));
			foreach($auditlist as $k=>$v){
				$update=array('id'=>$v['id'],'audit_type'=>AuditType::Zero[XPHEnum::Value]);
				$this->CI->Audit_model->update($update);
			}	
		}

		// 获取当前审核数据
		$audit=$this->CI->Audit_model->get_single(array('data_id'=>$data_id,'type'=>$type,'state'=>AuditState::UnAudit[XPHEnum::Value]));	

		if($audit['state']!=AuditState::UnAudit[XPHEnum::Value]){
			return (retJson('当前审核数据错误，请刷新重试',false));
		}

		if($audit['audit_type'] == AuditType::Zero[XPHEnum::Value]){ //审核类型为考核的时候		
			//审核等级为校级或分院级时,到分院审核过后就提交到考核库
			if($audit['examine_level'] == examine_level::Third[XPHEnum::Caption] || $audit['examine_level'] == examine_level::Fourth[XPHEnum::Caption]){
				// 1.修改审核为通过
				$update=array('id'=>$audit['id'],'state'=>AuditState::Pass[XPHEnum::Value],'operator'=>getSess()['id'],'audit_time'=>time());
				$this->CI->Audit_model->update($update);
				$this->updatePubliced($audit['data_id'],$type,$is_kaohe);//公开项目
				$this->CI->check_service->addCheckDetail($type,$audit['data_id']);//提交到考核库
			}else{
				// 1.修改审核为通过
				$update=array('id'=>$audit['id'],'state'=>AuditState::Pass[XPHEnum::Value],'operator'=>getSess()['id'],'audit_time'=>time());
				$this->CI->Audit_model->update($update);
				// 2.如果为分院审核则提交新增一条科研处审核
				if($audit['node']==AuditNode::First[XPHEnum::Value]){
					$add=array('admin_id'=>$audit['admin_id'],'type'=>$audit['type'],'data_id'=>$audit['data_id'],'data_name'=>$audit['data_name'],'audit_type'=>$audit['audit_type'],'examine_level'=>$audit['examine_level'],'node'=>AuditNode::Second[XPHEnum::Value],'state'=>AuditState::UnAudit[XPHEnum::Value],'created'=>time(),'dept_name'=>getSess()['dept_name'],'is_read'=>0);
					$this->CI->Audit_model->add($add);
				}
				// 3.如果为科研处审核，改变成果状态为公开
				if($audit['node']==AuditNode::Second[XPHEnum::Value]){
					$this->updatePubliced($audit['data_id'],$type,$is_kaohe);
					$this->CI->check_service->addCheckDetail($type,$audit['data_id']);//提交到考核库
				}
			}
		}else if($audit['audit_type'] == AuditType::First[XPHEnum::Value]){//审核类型为成果的时候
			// 1.修改审核为通过
			$update=array('id'=>$audit['id'],'state'=>AuditState::Pass[XPHEnum::Value],'operator'=>getSess()['id'],'audit_time'=>time());
			$this->CI->Audit_model->update($update);
			// 2.如果为分院审核则提交新增一条科研处审核
			if($audit['node']==AuditNode::First[XPHEnum::Value]){
				$add=array('admin_id'=>$audit['admin_id'],'type'=>$audit['type'],'data_id'=>$audit['data_id'],'data_name'=>$audit['data_name'],'audit_type'=>$audit['audit_type'],
				'examine_level'=>$audit['examine_level'],'node'=>AuditNode::Second[XPHEnum::Value],'state'=>AuditState::UnAudit[XPHEnum::Value],'created'=>time(),'dept_name'=>getSess()['dept_name'],'is_read'=>0);
				$this->CI->Audit_model->add($add);
			}
			// 3.如果为科研处审核，改变成果状态为公开
			if($audit['node']==AuditNode::Second[XPHEnum::Value]){
				$this->updatePubliced($audit['data_id'],$type,$is_kaohe);
				$this->CI->check_service->addCheckDetail($type,$audit['data_id']);//提交到考核库
			}

		}
		

		return (retJson('操作成功',true));
		
	}	

	//修改项目状态为已公开
	public function updatePubliced($data_id,$type,$is_kaohe){
		if($is_kaohe){//当审核为仅考核的时候将成果奖励修改为考核
			$update1=array('id'=>$data_id,'status'=>ResultState::Publiced[XPHEnum::Value],'is_submit_cg'=>AuditType::Zero[XPHEnum::Value]);
		}else{
			$update1=array('id'=>$data_id,'status'=>ResultState::Publiced[XPHEnum::Value]);
		}
		switch ($type) {
			case ResultsType::First[XPHEnum::Value]:
				$data = $this->CI->Paper_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}


				$this->CI->Paper_model->update($update1);
				break;
			
			case ResultsType::Second[XPHEnum::Value]:
				$data = $this->CI->Monograph_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Monograph_model->update($update1);
				break;
			
			case ResultsType::Third[XPHEnum::Value]:
				$data = $this->CI->Patent_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Patent_model->update($update1);
				break;
			
			case ResultsType::Fourth[XPHEnum::Value]:
				$data = $this->CI->Awards_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Awards_model->update($update1);
				break;
			
			case ResultsType::Fifth[XPHEnum::Value]:
				$data = $this->CI->Research_report_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Research_report_model->update($update1);
				break;
			
			case ResultsType::Sixth[XPHEnum::Value]:
				$data = $this->CI->Technical_standard_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Technical_standard_model->update($update1);
				break;
			
			case ResultsType::Seventh[XPHEnum::Value]:
				$data = $this->CI->Vertical_project_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Vertical_project_model->update($update1);
				break;
			
			case ResultsType::Eighth[XPHEnum::Value]:
				$data = $this->CI->Horizontal_project_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Horizontal_project_model->update($update1);
				break;
			
			case ResultsType::Ninth[XPHEnum::Value]:
				$data = $this->CI->Science_conversion_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Science_conversion_model->update($update1);
				break;
			
			case ResultsType::Tenth[XPHEnum::Value]:
				$data = $this->CI->Communication_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Communication_model->update($update1);
				break;
			
			case ResultsType::Eleventh[XPHEnum::Value]:
				$this->CI->Academic_report_model->update($update1);
				break;
			
			case ResultsType::Twelfth[XPHEnum::Value]:
				$this->CI->Academia_job_model->update($update1);
				break;
			case ResultsType::Thirteen[XPHEnum::Value]:
				$data = $this->CI->Subject_platform_model->get_single(array('id'=>$data_id));
				//更新32位唯一编码
				if($data['only_code']){	
					if($is_kaohe){
						$star_str = substr($data['only_code'],0,25);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.AuditType::Zero[XPHEnum::Value].ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}else{
						$star_str = substr($data['only_code'],0,26);
						$end_str = substr($data['only_code'],-5);
						$only_code = $star_str.ResultState::Publiced[XPHEnum::Value].$end_str; //更新审核状态
					}
					$update1['only_code'] = $only_code;
				}
				$this->CI->Subject_platform_model->update($update1);
				break;
		}

		return true;
	}


	/**  
	* 拒绝审核
	* @access public 
	* @param  $type  成果类型
	* @param  $data_id  成果id
	*/  
	function refused($type,$data_id,$remark=false){		
		// 获取当前审核数据
		$audit=$this->CI->Audit_model->get_single(array('data_id'=>$data_id,'type'=>$type,'state'=>AuditState::UnAudit[XPHEnum::Value]));
		if($audit['state']!=AuditState::UnAudit[XPHEnum::Value]){
			return (retJson('当前审核数据错误，请刷新重试',false));
		}
		// 1.修改审核为拒绝
		$update=array('id'=>$audit['id'],'state'=>AuditState::Refused[XPHEnum::Value],'operator'=>getSess()['id'],'audit_time'=>time());
		if($remark){
			$update['remark']=$remark;
		}
		$this->CI->Audit_model->update($update);
		
		// 2.改变成果状态为审核失败
		$update1=array('id'=>$audit['data_id'],'status'=>ResultState::Fail[XPHEnum::Value]);
		switch ($type) {
			case ResultsType::First[XPHEnum::Value]:
				$data = $this->CI->Paper_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Paper_model->update($update1);
				break;
			
			case ResultsType::Second[XPHEnum::Value]:
				$data = $this->CI->Monograph_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Monograph_model->update($update1);
				break;
			
			case ResultsType::Third[XPHEnum::Value]:
				$data = $this->CI->Patent_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Patent_model->update($update1);
				break;
			
			case ResultsType::Fourth[XPHEnum::Value]:
				$data = $this->CI->Awards_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Awards_model->update($update1);
				break;
			
			case ResultsType::Fifth[XPHEnum::Value]:
				$data = $this->CI->Research_report_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Research_report_model->update($update1);
				break;
			
			case ResultsType::Sixth[XPHEnum::Value]:
				$data = $this->CI->Technical_standard_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Technical_standard_model->update($update1);
				break;
			
			case ResultsType::Seventh[XPHEnum::Value]:
				$data = $this->CI->Vertical_project_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Vertical_project_model->update($update1);
				break;
			
			case ResultsType::Eighth[XPHEnum::Value]:
				$data = $this->CI->Horizontal_project_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Horizontal_project_model->update($update1);
				break;
			
			case ResultsType::Ninth[XPHEnum::Value]:
				$data = $this->CI->Science_conversion_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Science_conversion_model->update($update1);
				break;
			
			case ResultsType::Tenth[XPHEnum::Value]:
				$data = $this->CI->Communication_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Communication_model->update($update1);
				break;
			
			case ResultsType::Eleventh[XPHEnum::Value]:
				$this->CI->Academic_report_model->update($update1);
				break;
			
			case ResultsType::Twelfth[XPHEnum::Value]:
				$this->CI->Academia_job_model->update($update1);
				break;
			case ResultsType::Thirteen[XPHEnum::Value]:
				$data = $this->CI->Subject_platform_model->get_single(array('id'=>$data_id));
				if($data['only_code']){
					$star_str = substr($data['only_code'],0,26);
					$end_str = substr($data['only_code'],-5);
					$only_code = $star_str.ResultState::Fail[XPHEnum::Value].$end_str; //更新审核状态	
					$update1['only_code'] = $only_code;
				}
				$this->CI->Subject_platform_model->update($update1);
				break;
		}
		return (retJson('操作成功',true));
		
	}

	/**  
	* 获取审核记录
	* @access public 
	* @param  $type  成果类型
	* @param  $data_id  审核id
	*/  
	function auditProcess($type,$data_id){
		$connar['items']="db_audit.*,a1.username operator_name,a2.username submit_name";
		$connar['join'][0]=array('db_admin a1','a1.id=db_audit.operator');
		$connar['join'][1]=array('db_admin a2','a2.id=db_audit.admin_id');
		$connar['where']=$this->CI->sqlEscape("type=?? and data_id=??",array($type,$data_id));
		$connar['orderby']="created desc";
		$data=$this->CI->Audit_model->get_list_full($connar);
		foreach ($data as $k => $v) {
			$data[$k]['node_char']=(new AuditNode)->show($v['node']);
			$data[$k]['audit_time_char']=date('Y-m-d H:i:s',$v['audit_time']);
			$data[$k]['created_char']=date('Y-m-d H:i:s',$v['created']);
		}
		return $data;
	}

	//将考核转化为成果奖励
	function convert($type,$data_id){
		$auditlist=$this->CI->Audit_model->get_list(array('data_id'=>$data_id,'type'=>$type));
		foreach($auditlist as $k=>$v){
			$update=array('id'=>$v['id'],'audit_type'=>AuditType::First[XPHEnum::Value]);
			$this->CI->Audit_model->update($update);
		}

		$update1=array('id'=>$data_id,'is_submit_cg'=>AuditType::First[XPHEnum::Value]);
		switch ($type) {
			case ResultsType::First[XPHEnum::Value]:
				$data = $this->CI->Paper_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Paper_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('db_paper_kh.uuid=?? ', array('lw_'.$data_id));
					$kh = $this->CI->Paper_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Paper_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Paper_model->update($update1);
				break;
			
			case ResultsType::Second[XPHEnum::Value]:
				$data = $this->CI->Monograph_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Monograph_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('uuid=?? ', array('zz_'.$data_id));
					$kh = $this->CI->Monograph_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Monograph_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Monograph_model->update($update1);
				break;
			
			case ResultsType::Third[XPHEnum::Value]:
				$data = $this->CI->Patent_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Patent_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('uuid=?? ', array('zl_'.$data_id));
					$kh = $this->CI->Patent_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Patent_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Patent_model->update($update1);
				break;
			
			case ResultsType::Fourth[XPHEnum::Value]:
				$data = $this->CI->Awards_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Awards_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('uuid=?? ', array('hj_'.$data_id));
					$kh = $this->CI->Awards_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Awards_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Awards_model->update($update1);
				break;
			
			case ResultsType::Fifth[XPHEnum::Value]:
				$data = $this->CI->Research_report_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Research_report_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('uuid=?? ', array('yjbg_'.$data_id));
					$kh = $this->CI->Research_report_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Research_report_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Research_report_model->update($update1);
				break;
			
			case ResultsType::Sixth[XPHEnum::Value]:
				$data = $this->CI->Technical_standard_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Technical_standard_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('uuid=?? ', array('jsbz_'.$data_id));
					$kh = $this->CI->Technical_standard_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Technical_standard_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Technical_standard_model->update($update1);
				break;
			
			case ResultsType::Seventh[XPHEnum::Value]:
				$data = $this->CI->Vertical_project_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Vertical_project_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('new_uuid=?? ', array('zxkt_'.$data_id));
					$kh = $this->CI->Vertical_project_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Vertical_project_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Vertical_project_model->update($update1);
				break;
			
			case ResultsType::Eighth[XPHEnum::Value]:
				$data = $this->CI->Horizontal_project_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Horizontal_project_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('new_uuid=?? ', array('hxkt_'.$data_id));
					$kh = $this->CI->Horizontal_project_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Horizontal_project_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Horizontal_project_model->update($update1);
				break;
			
			case ResultsType::Ninth[XPHEnum::Value]:
				$data = $this->CI->Science_conversion_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Science_conversion_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('uuid=?? ', array('kjzh_'.$data_id));
					$kh = $this->CI->Science_conversion_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Science_conversion_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Science_conversion_model->update($update1);
				break;
			
			case ResultsType::Tenth[XPHEnum::Value]:
				$data = $this->CI->Communication_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Communication_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('uuid=?? ', array('xsjlh_'.$data_id));
					$kh = $this->CI->Communication_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Communication_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Communication_model->update($update1);
				break;
			
			case ResultsType::Eleventh[XPHEnum::Value]:
				$this->CI->Academic_report_model->update($update1);
				break;
			
			case ResultsType::Twelfth[XPHEnum::Value]:
				$this->CI->Academia_job_model->update($update1);
				break;
			case ResultsType::Thirteen[XPHEnum::Value]:
				$data = $this->CI->Subject_platform_model->get_single(array('id'=>$data_id));
				if($data['only_code'] != ''){
					$star_str = substr($data['only_code'],0,25);
					$end_str = substr($data['only_code'],-6);
					$only_code = $star_str.AuditType::First[XPHEnum::Value].$end_str; //更新是否成果奖励状态编码
					$update1['only_code'] = $only_code;
					//同步更改考核库业绩32位编码
					$this ->CI-> load -> model("Subject_platform_kh_model");
					$connar1['where'] = $this->CI->sqlEscape('uuid=?? ', array('xkpt_'.$data_id));
					$kh = $this->CI->Subject_platform_kh_model->get_single_full($connar1);
					if($kh){
						$update2['id'] = $kh['id'];
						$update2['only_code'] = $only_code;
						$this->CI->Subject_platform_kh_model->update($update2,'id'); //修改考核库
					}
				}
				$this->CI->Subject_platform_model->update($update1);
				break;
		}
		
		return (retJson('操作成功',true));
	}
	
}